﻿//document.observe("dom:loaded", StartSlideIndex);
//function StartSlideIndex() {
//    if ($("dListIMGSlideShow")) {
//        setTimeout("setSlider()", 3000);
//        
//    }
//}
var id, idx, i, wS, _style, auto, mvtime, step, sTime, call;
var nxtDisable, nxtAction, preDisable, preAction;
var arg = new Array();
var argThumbs = new Array();
function setSlider() {
    //Mr. Hieu 01/9/2011
    //    var mvtime: là thời gian dịch chuyển slide, 
    //    var sTime:  là thời gian tam dừng của slide khi slide ở vị trí chính giữa.
    //    Lưu ý khi thay đổi các   ID:             dListIMGSlideShow, thumbs
    //    Lưu ý khi thay đổi các   selector:       a, li
    //    nxtDisable = "img_slide/btnNext2.png";
    //    nxtAction = "img_slide/btnNext.png";
    //    preDisable = "img_slide/btnPreview.png";
    //    preAction = "img_slide/btnPreview2.png";

    mvtime = 17; sTime = 3000;

    var strInner = "";
    var strHref = "";
    var strInnerNode = "";
    i = 0; $('dListIMGSlideShow').adjacent('a').each(function(node) { strHref = node.href; node.setAttribute("id", "s" + (i + 1).toString()); i += 1; }); i = 0;
    for (i = 0; i < arrPhotoSlide.length; i++) {
        if (i != 0) {
            var aLink = document.createElement("a");
            aLink.href = strHref;
            aLink.setAttribute("id", "s" + (i + 1).toString());
            aLink.innerHTML = "<img height='259' width='418' alt='' title='' src='" + arrPhotoSlide[i] + "' itemprop='image' />";
            document.getElementById("dListIMGSlideShow").appendChild(aLink);
        }
        strInnerNode += "<li class='" + ((i == 0) ? "activeSlide" : "") + "' id='i" + (i + 1).toString() + "' onclick='selectSlide(" + i.toString() + ")' >";
        strInnerNode += "<a name='' href='javascript:void(0);'>" + (i + 1).toString() + "</a>";
        strInnerNode += "</li>";
    }
    var aLink = document.createElement("a");
    aLink.href = strHref;
    aLink.setAttribute("id", "s" + (i + 1).toString());
    aLink.innerHTML = "<img height='259' width='418' alt='' title='' src='" + arrPhotoSlide[0] + "' itemprop='image' />";
    document.getElementById("dListIMGSlideShow").appendChild(aLink);

    $("thumbs").innerHTML = strInnerNode;

    id = 0; idx = 0; i = 0; auto = true;
    var arr = $('dListIMGSlideShow').adjacent('a');
    arr.each(function(node) { arg[i] = node.id; i += 1; }); i = 0;

    arr = $('thumbs').adjacent('li');
    arr.each(function(node) { argThumbs[i] = node.id; i += 1; }); i = 0;
    wS = $(arg[0]).getWidth(); step = Math.round(wS / (mvtime * 1.2));
    $("dListIMGSlideShow").observe("mouseout", setAuto); $("thumbs").observe("mouseout", setAuto);
    call = setTimeout("loadSlider()", sTime);
}
function setAuto() { if (!auto) { clearTimeout(call); auto = true; call = setTimeout("loadSlider()", sTime); } }
function selectSlide(s) {
    clearTimeout(call);
    auto = false;
    if (s == "pre" || s == "nxt") {
        try { $(arg[idx]).setStyle({ margin: "0 0 0 " + -wS.toString(), display: "block" }); } catch (e) { }
        if (idx >= arg.length - 1 && s == "nxt") { $("nxt").src = nxtDisable; } else { $("nxt").src = nxtAction; }
        if (idx <= 0 && s == "pre") { $("pre").src = preDisable; } else { $("pre").src = preAction; }
        s = (s == "nxt") ? idx + 1 : ((idx == 0) ? idx : ((i < 0) ? idx : idx - 1));
        if (s < 0 || s > arg.length - 1) { return; }
    } 
    setThumbs(s);
    if (s >= idx) {
        idx = (i <= -wS) ? idx + 1 : idx;
        i = (i <= -wS) ? 0 : (i - step * 5);
        id = arg[idx];
        $(id).setStyle({ margin: "0 0 0 " + i.toString() + "px", display: "block" });
        if (s > idx) { call = setTimeout("selectSlide(" + s + ")", mvtime); }
        else { i = 0; idx = s; id = arg[s]; $(id).setStyle({ margin: "0", display: "block" }); }
    } else {
        for (i = 0; i < arg.length; i++) { id = arg[i]; $(id).setStyle({ margin: "auto", display: "block" }); }
        for (i = 0; i < s; i++) { $(arg[i]).setStyle({ margin: "0 0 0 " + -wS.toString() + "px", display: "block" }); }
        idx = s; i = 0; $(arg[idx]).setStyle({ margin: "0", display: "block" });
    }
}
function resetSlider() {
    idx = 0; auto = true;
    for (i = 0; i < arg.length - 1; i++) { $(arg[i]).setStyle({ margin: "auto" }); } i = 0; 
    call = setTimeout("loadSlider()", mvtime);
}
function setThumbs(s) {
    //try { $('thumbs').select('li').invoke('removeClassName', 'activeSlide'); } catch (e) { }
    try { for (var j = 0; j < argThumbs.length; j++) { $(argThumbs[j]).removeClassName("activeSlide"); } } catch (e) { }
    try { $(argThumbs[s]).addClassName("activeSlide"); } catch (e) { }
}
function loadSlider() {
    clearTimeout(call);
    if (!auto) { return; }
    if (i == 0) { setThumbs(idx + 1); }
    idx = (i <= -wS) ? idx + 1 : idx;
    i = (i <= -wS) ? 0 : (i - step);
    try {
        if (idx >= arg.length - 1) {
            clearTimeout(call);
            resetSlider();
        }
        else {
            if (idx >= arg.length - 2) {
                setThumbs(0);
            }
            _style = "0 0 0 " + i.toString() + "px";
            $(arg[idx]).setStyle({ margin: (!auto) ? "0" : _style });
            //        if (idx >= arg.length - 1) { $("nxt").src = nxtDisable; } else $("nxt").src = nxtAction;
            //        if (idx <= 0) { $("pre").src = preDisable; } else $("pre").src = preAction;
            call = setTimeout("loadSlider()", (i <= -wS) ? (idx == arg.length - 1) ? mvtime : sTime : mvtime);
        }
    } catch (e) { clearTimeout(call); resetSlider(); }
}